﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using CMISLibrary.Libraries;
using BaocaoNPC.Usercontrols.reports;
using BaocaoNPC.common;
using CMISLibrary;
using BCNPCEntities;
using System.Threading;

namespace CMIS2.BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for PC1_BCKD_5B.xaml
    /// </summary>
    public partial class PC1_BCKD_5B : UserControl
    {
        /*
        private string strMa_dviqly = UserInfomation.MaDViqly != "" && UserInfomation.MaDViqly != null ? UserInfomation.MaDViqly : "PA11";
        private string strTenDNHap = UserInfomation.TenDNHap != "" && UserInfomation.TenDNHap != null ? UserInfomation.TenDNHap : "PD0400";
        private string strCurrentLibID = MenuInfomation.LibID != "" && MenuInfomation.LibID != null ? MenuInfomation.LibID : "65";
        private string strTen_dviqly = UserInfomation.TenDViQLy != "" && UserInfomation.TenDViQLy != null ? UserInfomation.TenDViQLy : "Dien luc ...";
        private string strTen_dviqly_CT = UserInfomation.TenDViQLy_CT != "" && UserInfomation.TenDViQLy_CT != null ? UserInfomation.TenDViQLy_CT : "Cong ty Dien luc ...";
        */

        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";

        public Int16 p_thang = 0;
        public Int16 p_nam = 0;

        private string p_stt = "";
        private string p_danh_muc = "";
        private string p_maso = "";
        private Decimal p_phat_sinh = 0;        
        private Decimal p_luy_ke = 0;
        private string p_ghi_chu = "";

        Int16 i16ThangBC = 0;
        Int16 i16NamBC = 0;

        Int16 i16ThangInBC = 0;
        Int16 i16NamInBC = 0;
        String strMa_dviqly_InBC = "";
        String strTen_dviqly_InBC = "";
        String strTen_dviqly_CT_InBC = "";

        int intFormLoad = 0;
        public DataSet ds = new DataSet();
        DataSet ds_BCKD_In = new DataSet();
        DataSet ds_BCKD_QTTH = new DataSet();
        String strInBieuBCKD = "";
        //public String p_madviqly = CMISLibrary.Libraries.UserInfomation.MaDViqly.ToString();


        private void GET_THANGNAM()
        {
            try
            {
                //DataTable dtThangNam = Utility.getThangNamCurrent("BC");
                DataSet dsthanglviec = new DataSet();                
                dsthanglviec = GlobalServices.Service_NPCUS.get_thang_lviec();
                if (dsthanglviec != null && dsthanglviec.Tables.Count > 0)
                {
                    foreach (DataRow dr in dsthanglviec.Tables[0].Rows)
                        if (dr["PHAN_HE"].ToString() == "BC" && dr["MA_DVIQLY"].ToString() == strMa_dviqly)
                        {
                            i16ThangBC = Convert.ToInt16(dr["THANG"]);
                            i16NamBC = Convert.ToInt16(dr["NAM"]);
                        }
                }

            }
            catch
            {
                //i16ThangBC = 10;
                //i16NamBC = 2009;
                i16ThangBC = Convert.ToInt16(DateTime.Now.Month);
                i16NamBC = Convert.ToInt16(DateTime.Now.Year);
            }
        }


        public PC1_BCKD_5B()
        {
            InitializeComponent();

            btnDanhmuc.IsEnabled = true;
            btnSua.IsEnabled = false;
            btnGhi.IsEnabled = false;
            btnHuy.IsEnabled = false;
            btnIn.IsEnabled = false;
            btnNhapsolieu.IsEnabled = false;

            //enableTextbox(false);
            GET_THANGNAM();


            //InitializeComponent();
            if (i16ThangBC.ToString().Length == 1)
            {
                dtpThangNam.Text = "0" + i16ThangBC.ToString() + "/" + i16NamBC.ToString();
                //dtpThangNam_1.Text = "0" + i16ThangBC.ToString() + "/" + i16NamBC.ToString();
            }
            else
            {
                dtpThangNam.Text = i16ThangBC.ToString() + "/" + i16NamBC.ToString();
                //dtpThangNam_1.Text = i16ThangBC.ToString() + "/" + i16NamBC.ToString();
            }
        }

        private void btnSua_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dgrBIEU5B.Columns[0].IsReadOnly = true;
                dgrBIEU5B.Columns[1].IsReadOnly = true;
                dgrBIEU5B.Columns[2].IsReadOnly = true;
                dgrBIEU5B.IsEnabled = true;


                btnDanhmuc.IsEnabled = false;
                btnSua.IsEnabled = false;
                btnGhi.IsEnabled = true;
                btnHuy.IsEnabled = true;
                btnIn.IsEnabled = false;
                btnNhapsolieu.IsEnabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btnGhi_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //bool check = false;
                p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);

                DataView dv = dgrBIEU5B.ItemsSource as DataView;
                DataTable dt = dv.Table;
                //cls_PC1_BCKD_5B cls = new cls_PC1_BCKD_5B();                



                //------- ghi du lieu -------
                GlobalServices.Service_NPCUS.PC1_BIEU5B_DEL(strMa_dviqly, p_thang, p_nam);
                //cls.PC1_BIEU5B_DEL(strMa_dviqly, p_thang, p_nam);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    p_stt = dt.Rows[i]["STT"].ToString();
                    p_maso = dt.Rows[i]["MA_SO"].ToString();
                    p_danh_muc = dt.Rows[i]["DANH_MUC"].ToString();

                    if (dt.Rows[i]["PHAT_SINH"].ToString().Trim() == string.Empty)
                    {
                        p_phat_sinh = 0;
                    }
                    else
                    {
                        p_phat_sinh = Convert.ToDecimal(dt.Rows[i]["PHAT_SINH"]);
                    }

                    if (dt.Rows[i]["LUY_KE"].ToString().Trim() == string.Empty)
                    {
                        p_luy_ke = 0;
                    }
                    else
                    {
                        p_luy_ke = Convert.ToDecimal(dt.Rows[i]["LUY_KE"]);
                    }

                    if (dt.Rows[i]["GHI_CHU"].ToString().Trim() == string.Empty)
                    {
                        p_ghi_chu = "";
                    }
                    else
                    {
                        p_ghi_chu = Convert.ToString(dt.Rows[i]["GHI_CHU"]);
                    }

                    GlobalServices.Service_NPCUS.PC1_BIEU5B_IN(strMa_dviqly, p_thang, p_nam, p_stt, p_danh_muc, p_maso,p_phat_sinh,p_luy_ke,p_ghi_chu);


                }
                GlobalServices.Service_NPCUS.PC1_BIEU5B_UP(strMa_dviqly, p_thang, p_nam);

                //---------- kiem tra ghi du lieu---------
                ds.Clear();
                ds = GlobalServices.Service_NPCUS.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                //ds = cls.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                //ds.WriteXmlSchema("C:\\PC1_BCKD5B.xsd");
                if (ds != null)
                {
                    ds.Tables[0].DefaultView.AllowNew = false;
                    dgrBIEU5B.ItemsSource = ds.Tables[0].DefaultView;
                    dgrBIEU5B.Columns[0].IsReadOnly = true;
                    dgrBIEU5B.Columns[1].IsReadOnly = true;
                    dgrBIEU5B.Columns[2].IsReadOnly = true;
                    MessageBox.Show("Ghi dữ liệu thành công ! ", "Thông báo", MessageBoxButton.OK);
                    btnDanhmuc.IsEnabled = true;
                    btnSua.IsEnabled = true;
                    btnGhi.IsEnabled = false;
                    btnHuy.IsEnabled = false;
                    btnIn.IsEnabled = true;
                    btnNhapsolieu.IsEnabled = false;
                }
                else
                {
                    MessageBox.Show("Không ghi được dữ liệu !", "Thông báo", MessageBoxButton.OK);
                    return;
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString() + " số liệu nhập vào quá  lớn ! ");
            }

        }


        private void btnIn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet dsIn = new DataSet();

                if (strMa_dviqly.Length == 6)
                {

                    //cls_PC1_BCKD_5B cls = new cls_PC1_BCKD_5B();
                    //p_nam = 0;
                    //p_thang = 0;
                    p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                    p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
                    //cls.PC1_BIEU5B_UP(strMa_dviqly, p_thang, p_nam);
                    //dsIn = cls.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                    dsIn = GlobalServices.Service_NPCUS.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                    //ds.Tables[0].TableName = "danhsachIn";

                    if (dsIn != null)
                    {
                        DataTable dt = dsIn.Tables[0].Select("1=1", "MA_SO ASC").CopyToDataTable();
                        rpt_PC1_BCKD5B rpt = new rpt_PC1_BCKD5B();
                        rpt.SetDataSource(dt);
                        //string[] dd;
                        //string[] bb;
                        //dd = new string[] { "NguoiLapBieu", "KyGhiRoHoVaTen_NLB", "Chucvukyduyet", "KyGhiRoHoVaTen_CVKD" };
                        //bb = new string[] { "A", "B", "C", "D" };
                        //rpt.SetParameterValue("nguoilapbieu", strTenDNHap);
                        //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly, dd, dd);
                        UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Nguoilapbieu", "Trinhky" }, new string[] { "Nguoilapbieu", "Trinhky" }, "CMIS2 - Report: Báo cáo NPC");
                        dsIn.Clear();
                        //var test = dsIn.Tables.Count.ToString();                      
                    }
                    else
                    {
                        MessageBox.Show("Tháng " + p_thang + " năm " + p_nam + " không có dữ liệu !", "Thông báo", MessageBoxButton.OK);
                    }
                    // dsIn.WriteXmlSchema("c:\\test.ttx");
                }
                else
                {
                    p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                    p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
                    //cls.PC1_BIEU5B_UP(strMa_dviqly, p_thang, p_nam);
                    //dsIn = cls.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                    dsIn = GlobalServices.Service_NPCUS.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                    //ds.Tables[0].TableName = "danhsachIn";

                    if (dsIn != null)
                    {
                        DataTable dt = dsIn.Tables[0].Select("1=1", "MA_SO ASC").CopyToDataTable();
                        rpt_PC1_BCKD5B rpt = new rpt_PC1_BCKD5B();          
                        rpt.SetDataSource(dt);
                        //string[] dd;
                        //string[] bb;
                        //dd = new string[] { "NguoiLapBieu", "KyGhiRoHoVaTen_NLB", "Chucvukyduyet", "KyGhiRoHoVaTen_CVKD" };
                        //bb = new string[] { "A", "B", "C", "D" };
                        //rpt.SetParameterValue("nguoilapbieu", strTenDNHap);
                        //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly, dd, dd);
                        UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Nguoilapbieu", "Trinhky" }, new string[] { "Nguoilapbieu", "Trinhky" }, "CMIS2 - Report: Báo cáo NPC");
                        dsIn.Clear();
                        //var test = dsIn.Tables.Count.ToString();
                    }
                    else
                    {
                        MessageBox.Show("Tháng " + p_thang + " năm " + p_nam + " không có dữ liệu !", "Thông báo", MessageBoxButton.OK);
                    }
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
        }



        private void btnHuy_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnDanhmuc.IsEnabled = true;
                btnSua.IsEnabled = false;
                btnGhi.IsEnabled = false;
                btnHuy.IsEnabled = false;
                btnIn.IsEnabled = false;
                btnNhapsolieu.IsEnabled = false;

                dgrBIEU5B.Columns[0].IsReadOnly = true;
                dgrBIEU5B.Columns[1].IsReadOnly = true;
                dgrBIEU5B.Columns[2].IsReadOnly = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }



        private void btnDanhmuc_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                #region //====== Cap dien luc ======
                if ((strMa_dviqly.Length == 6) || (strMa_dviqly.Length == 4 && !strMa_dviqly.Contains("PA")))
                {
                    if (ds != null)
                    {
                        ds.Clear();
                    }

                    //cls_PC1_BCKD_5B cls = new cls_PC1_BCKD_5B();
                    p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                    p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
                    ds = GlobalServices.Service_NPCUS.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                    //ds = cls.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                    //ds.Tables[0].TableName = "danhsachIn";

                    if (ds == null) //---- chua co du lieu cua thang --------
                    {
                        if (p_thang == i16ThangBC && p_nam == i16NamBC)  //----- thang bao cao chua co dlieu -> nhap dmuc ----
                        {
                            //MessageBox.Show("Tháng " + p_thang + " năm " + p_nam + " chưa có dữ liệu !", "Thông báo", MessageBoxButton.OK);
                            //ds = cls.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, "DM", 0, 0);
                            ds = GlobalServices.Service_NPCUS.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, "DM", 0, 0);
                            //ds.WriteXmlSchema("c:\\PC1_BCKD5B.xsd");
                            if (ds == null)
                            {
                                MessageBox.Show("Không có danh mục !");
                                return;
                            }
                            ds.Tables[0].DefaultView.AllowNew = false;
                            ds.Tables[0].DefaultView.AllowDelete = true;
                            dgrBIEU5B.ItemsSource = ds.Tables[0].DefaultView;
                            dgrBIEU5B.Columns[0].IsReadOnly = true;
                            dgrBIEU5B.Columns[1].IsReadOnly = true;
                            dgrBIEU5B.Columns[2].IsReadOnly = true;

                            btnDanhmuc.IsEnabled = true;
                            btnSua.IsEnabled = false;
                            btnGhi.IsEnabled = false;
                            btnHuy.IsEnabled = false;
                            btnIn.IsEnabled = true;
                            btnNhapsolieu.IsEnabled = true;
                            dgrBIEU5B.IsEnabled = false;
                        }
                        else
                        {
                            btnDanhmuc.IsEnabled = true;
                            btnSua.IsEnabled = false;
                            btnGhi.IsEnabled = false;
                            btnHuy.IsEnabled = false;
                            btnIn.IsEnabled = false;
                            btnNhapsolieu.IsEnabled = false;
                            MessageBox.Show("Không phải tháng báo cáo, tháng báo cáo là: " + i16ThangBC + "/" + i16NamBC, "Thông báo", MessageBoxButton.OK);
                        }

                    }
                    else
                    {      //--------- da co du lieu -------- 

                        //-------- chi cho sua du lieu tai thang bao cao --------
                        if (p_thang == i16ThangBC && p_nam == i16NamBC)
                        {
                            btnSua.IsEnabled = true;
                        }
                        else btnSua.IsEnabled = false;

                        btnDanhmuc.IsEnabled = true;
                        btnGhi.IsEnabled = false;
                        btnHuy.IsEnabled = false;
                        btnIn.IsEnabled = true;
                        btnNhapsolieu.IsEnabled = false;

                        ds.Tables[0].DefaultView.AllowNew = false;
                        dgrBIEU5B.ItemsSource = ds.Tables[0].DefaultView;
                        dgrBIEU5B.Columns[0].IsReadOnly = true;
                        dgrBIEU5B.Columns[1].IsReadOnly = true;
                        dgrBIEU5B.Columns[2].IsReadOnly = true;
                    }
                }
                #endregion
                #region  // ====== cong ty dien luc ========
                else  // ====== cong ty dien luc ========
                {
                    if ((strMa_dviqly.Length == 4 && strMa_dviqly.Contains("PA")) || (strMa_dviqly.Length == 2 && !strMa_dviqly.Contains("PA")))  // ====== cong ty dien luc ========                    
                    {
                        if (ds != null)
                        {
                            ds.Clear();
                        }

                        //cls_PC1_BCKD_5B cls = new cls_PC1_BCKD_5B();
                        p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                        p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
                        ds = GlobalServices.Service_NPCUS.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                        //ds = cls.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);
                        //ds.Tables[0].TableName = "danhsachIn";

                        if (ds == null) //---- chua co du lieu cua thang --------
                        {
                            if (p_thang == i16ThangBC && p_nam == i16NamBC)  //----- thang bao cao chua co dlieu -> nhap dmuc ----
                            {
                                //MessageBox.Show("Tháng " + p_thang + " năm " + p_nam + " chưa có dữ liệu !", "Thông báo", MessageBoxButton.OK);
                                //ds = cls.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, "DM", 0, 0);
                                ds = GlobalServices.Service_NPCUS.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, "DM", 0, 0);
                                if (ds == null)
                                {
                                    MessageBox.Show("Không có danh mục !");
                                    return;
                                }
                                ds.Tables[0].DefaultView.AllowNew = false;
                                ds.Tables[0].DefaultView.AllowDelete = true;
                                dgrBIEU5B.ItemsSource = ds.Tables[0].DefaultView;
                                dgrBIEU5B.Columns[0].IsReadOnly = true;
                                dgrBIEU5B.Columns[1].IsReadOnly = true;
                                dgrBIEU5B.Columns[2].IsReadOnly = true;

                                DataView dv = dgrBIEU5B.ItemsSource as DataView;
                                DataTable dt = dv.Table;

                                //------- ghi du lieu -------
                                //GlobalServices.Service_NPCUS.PC1_BIEU5B_DEL(strMa_dviqly, p_thang, p_nam);
                                //cls.PC1_BIEU5B_DEL(strMa_dviqly, p_thang, p_nam);
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    p_stt = dt.Rows[i]["STT"].ToString();
                                    p_maso = dt.Rows[i]["MA_SO"].ToString();
                                    p_danh_muc = dt.Rows[i]["DANH_MUC"].ToString();

                                    if (dt.Rows[i]["PHAT_SINH"].ToString().Trim() == string.Empty)
                                    {
                                        p_phat_sinh = 0;
                                    }
                                    else
                                    {
                                        p_phat_sinh = Convert.ToDecimal(dt.Rows[i]["PHAT_SINH"]);
                                    }

                                    if (dt.Rows[i]["LUY_KE"].ToString().Trim() == string.Empty)
                                    {
                                        p_luy_ke = 0;
                                    }
                                    else
                                    {
                                        p_luy_ke = Convert.ToDecimal(dt.Rows[i]["LUY_KE"]);
                                    }

                                    if (dt.Rows[i]["GHI_CHU"].ToString().Trim() == string.Empty)
                                    {
                                        p_ghi_chu = "";
                                    }
                                    else
                                    {
                                        p_ghi_chu = Convert.ToString(dt.Rows[i]["GHI_CHU"]);
                                    }


                                    GlobalServices.Service_NPCUS.PC1_BIEU5B_IN(strMa_dviqly, p_thang, p_nam, p_stt, p_danh_muc, p_maso, p_phat_sinh, p_luy_ke, p_ghi_chu);

                                }
                                GlobalServices.Service_NPCUS.PC1_BIEU5B_UP(strMa_dviqly, p_thang, p_nam);

                                DataSet dsTemp = GlobalServices.Service_NPCUS.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);

                                //DataView dv = dsTemp.Tables[0].Select("1=1","ORDER MA_SO").DefaultView;
                                if (dsTemp == null)
                                {
                                    MessageBox.Show("Chưa có dữ liệu của Điện lực");
                                    return;
                                }

                                DataTable dt1 = dsTemp.Tables[0].Select("1=1", "MA_SO ASC").CopyToDataTable();

                                dt1.DefaultView.AllowNew = false;
                                dgrBIEU5B.ItemsSource = dt1.DefaultView;
                                dgrBIEU5B.Columns[0].IsReadOnly = true;
                                dgrBIEU5B.Columns[1].IsReadOnly = true;
                                dgrBIEU5B.Columns[2].IsReadOnly = true;


                                btnDanhmuc.IsEnabled = true;
                                btnSua.IsEnabled = false;
                                btnGhi.IsEnabled = false;
                                btnHuy.IsEnabled = false;
                                btnIn.IsEnabled = true;
                                btnNhapsolieu.IsEnabled = false;
                                dgrBIEU5B.IsEnabled = false;
                            }
                            else
                            {
                                btnDanhmuc.IsEnabled = true;
                                btnSua.IsEnabled = false;
                                btnGhi.IsEnabled = false;
                                btnHuy.IsEnabled = false;
                                btnIn.IsEnabled = false;
                                btnNhapsolieu.IsEnabled = false;
                                MessageBox.Show("Không phải tháng báo cáo, tháng báo cáo là: " + i16ThangBC + "/" + i16NamBC, "Thông báo", MessageBoxButton.OK);
                            }

                        }
                        else
                        {      //--------- da co du lieu -------- 
                            DataSet dsTemp;
                            //-------- chi cho sua du lieu tai thang bao cao --------
                            if (p_thang == i16ThangBC && p_nam == i16NamBC)
                            {
                                //btnSua.IsEnabled = true;
                                GlobalServices.Service_NPCUS.PC1_BIEU5B_UP(strMa_dviqly, p_thang, p_nam);
                                dsTemp = GlobalServices.Service_NPCUS.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);

                                if (dsTemp == null)
                                {
                                    MessageBox.Show("Chưa có dữ liệu của Điện lực");
                                    return;
                                }
                                btnSua.IsEnabled = false;
                            }
                            else
                            {
                                btnSua.IsEnabled = false;
                                dsTemp = GlobalServices.Service_NPCUS.select_BCKD5B(strTen_dviqly, strTen_dviqly_CT, strMa_dviqly, p_thang, p_nam);

                                if (dsTemp == null)
                                {
                                    MessageBox.Show("Không có dữ liệu !");
                                    return;
                                }
                            }

                            btnDanhmuc.IsEnabled = true;
                            btnGhi.IsEnabled = false;
                            btnHuy.IsEnabled = false;
                            btnIn.IsEnabled = true;
                            //btnNhapsolieu.IsEnabled = false;
                            DataTable dt = dsTemp.Tables[0].Select("1=1", "MA_SO ASC").CopyToDataTable();

                            dt.DefaultView.AllowNew = false;
                            //dgrBIEU5B.ItemsSource = dsTemp.Tables[0].Select("1=1", "ORDER MA_SO");
                            dgrBIEU5B.ItemsSource = dt.DefaultView;
                            dgrBIEU5B.Columns[0].IsReadOnly = true;
                            dgrBIEU5B.Columns[1].IsReadOnly = true;
                            dgrBIEU5B.Columns[2].IsReadOnly = true;
                            //dgrBIEU5B.Columns[0].IsReadOnly = f;
                            //dgrBIEU5B.Columns[0].IsReadOnly = true;
                            //txtGhichu.Text = ds.Tables[0].Rows[0]["GHI_CHU"].ToString();
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString() + " Kiểm tra lại kết nối ! ");
            }
        }

        private void btnNhapsolieu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dgrBIEU5B.Columns[0].IsReadOnly = true;
                dgrBIEU5B.Columns[1].IsReadOnly = true;
                dgrBIEU5B.Columns[2].IsReadOnly = true;
                dgrBIEU5B.IsEnabled = true;

                btnDanhmuc.IsEnabled = false;
                btnSua.IsEnabled = false;
                btnGhi.IsEnabled = true;
                btnHuy.IsEnabled = true;
                btnIn.IsEnabled = false;
                btnNhapsolieu.IsEnabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
